from .champion import Rider
from typing import Dict, Any, Optional


class WeiQing(Rider):
    """卫青 - 基础骑士"""
    
    def __init__(self):
        super().__init__(
            card_id="wei_qing",
            name="Wei Qing",
            image_path="Wei Qing.jpg",
            description="Rider: Can attack the turn it is summoned",
            cost=1,
            attack=1,
            hp=1
        )


class MengTian(Rider):
    """蒙恬 - 强力骑士"""
    
    def __init__(self):
        super().__init__(
            card_id="meng_tian",
            name="Meng Tian",
            image_path="Meng Tian.jpg",
            description="Rider: Can attack the turn it is summoned",
            cost=8,
            attack=7,
            hp=5
        )


class QinLiangyu(Rider):
    """秦良玉 - 基础骑士"""
    
    def __init__(self):
        super().__init__(
            card_id="qin_liangyu",
            name="Qin Liangyu",
            image_path="Qin Liangyu.jpg",
            description="Rider: Can attack the turn it is summoned",
            cost=5,
            attack=4,
            hp=5
        )


class YingZheng(Rider):
    """嬴政 - 高费骑士"""
    
    def __init__(self):
        super().__init__(
            card_id="ying_zheng",
            name="Ying Zheng",
            image_path="Ying Zheng.jpg",
            description="Rider: Can attack the turn it is summoned",
            cost=10,
            attack=9,
            hp=10
        )


class SunQuan(Rider):
    """孙权 - 召唤时场上每有一个友方Guard获得+2/+2"""
    
    def __init__(self):
        super().__init__(
            card_id="sun_quan",
            name="Sun Quan",
            image_path="Sun Quan.jpg",
            description="Rider: Can attack the turn it is summoned.\nOn summon: For each friendly Guard, gain +2/+2",
            cost=4,
            attack=2,
            hp=1
        )
    
    def on_summon(self, game_state, player_id: str):
        """召唤时场上每有一个友方Guard获得+2/+2"""
        battlefield = game_state.battlefields[player_id]
        
        # 统计友方Guard数量
        guard_count = 0
        for champion in battlefield:
            if champion and champion.is_alive():
                # 检查是否为Guard类型（通过card_id判断）
                guard_cards = ["test_guard", "zhen_ji", "sun_ce", "zhou_yu", "zhang_liang", 
                              "hua_mulan", "liu_bei", "lu_ban", "diao_chan", "li_shuwen", "liu_bang"]
                if champion.card_id in guard_cards:
                    guard_count += 1
        
        # 根据Guard数量增加自身属性
        self.attack += guard_count * 2
        self.hp += guard_count * 2


class LiangHongyu(Rider):
    """梁红玉 - 基础骑士"""
    
    def __init__(self):
        super().__init__(
            card_id="liang_hongyu",
            name="Liang Hongyu",
            image_path="Liang Hongyu.jpg",
            description="Rider: Can attack the turn it is summoned",
            cost=2,
            attack=2,
            hp=1
        )


class XuFu(Rider):
    """徐福 - 每次攻击敌方召唤师获得+1攻击"""
    
    def __init__(self):
        super().__init__(
            card_id="xu_fu",
            name="Xu Fu",
            image_path="Xu Fu.jpg",
            description="Rider: Can attack the turn it is summoned.\nEach time you attack an enemy summoner, gain +1 attack",
            cost=2,
            attack=1,
            hp=2
        )
    
    def on_deal_damage(self, game_state, target, damage: int, player_id: str):
        """每次攻击敌方召唤师获得+1攻击"""
        # 检查目标是否为召唤师（通过检查是否有hp属性但没有attack属性）
        is_summoner = (hasattr(target, 'hp') and not hasattr(target, 'attack') and 
                      hasattr(target, 'card_id') and target.card_id.endswith('_summoner'))
        
        if is_summoner:
            # 检查目标是否为敌方召唤师
            target_player = None
            for p_id in game_state.players:
                if target == game_state.get_summoner(p_id):
                    target_player = p_id
                    break
            
            if target_player and target_player != player_id:
                self.attack += 1


class DaJi(Rider):
    """妲己 - 召唤时场上每有一个友方rider获得+1攻击"""
    
    def __init__(self):
        super().__init__(
            card_id="da_ji",
            name="Da Ji",
            image_path="Da Ji.jpg",
            description="Rider: Can attack the turn it is summoned.\nOn summon: For each friendly Rider, gain +1 attack",
            cost=2,
            attack=1,
            hp=2
        )
    
    def on_summon(self, game_state, player_id: str):
        """召唤时场上每有一个友方rider获得+1攻击"""
        battlefield = game_state.battlefields[player_id]
        
        # 统计友方rider数量
        rider_count = 0
        rider_cards = ["test_rider", "wei_qing", "meng_tian", "qin_liangyu", "ying_zheng", 
                       "sun_quan", "liang_hongyu", "xu_fu", "da_ji"]
        for champion in battlefield:
            if champion and champion.is_alive():
                if champion.card_id in rider_cards:
                    rider_count += 1
        
        # 根据rider数量增加攻击
        self.attack += rider_count
